import axios, {AxiosInstance} from "axios";

const isDev = process.env.NODE_ENV === 'development'

const myAxios: AxiosInstance =  axios.create({
    baseURL: isDev ? 'http://localhost:9090/api':'http://www.ckdiligent.cn',
    // timeout: 100000,
});
myAxios.defaults.withCredentials = true;
myAxios.interceptors.request.use((config)=>{
    console.log('我要发请求啦')
    return config;
},(error)=>{
    return Promise.reject(error);
});

myAxios.interceptors.response.use((response)=>{
    console.log('响应的信息',response)
    if (response.data?.code === 40100){
        const redirectUrl = window.location.href;
        //未登录，跳转到登录页
        window.location.href = `/user/login?redirect=${redirectUrl}`;
    }
    return response;
},(error)=>{
    return Promise.reject(error)
});

export default myAxios;
